package com.oocl.produce.objecttojson;


import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.oocl.produce.pojo.Book;

public class ObjectToJson {


	private static String objectToJson(Book book) {
		String jsoString = null;
		JSONObject jso = new JSONObject();
		jso.put("id", book.getId());
		jso.put("name", book.getName());
		jso.put("publicationDate", book.getPublicationDate().getTime());
		jso.put("author", book.getAuthor());
		jso.put("price", book.getPrice());
		jso.put("fineness", book.getFineness());
		jso.put("publisher", book.getPublisher());
		jsoString = jso.toString();
		return jsoString;
	}
	
	
	public static List<String> toJsonStrings(String path) throws Exception{
		ObjectInputStream input=new ObjectInputStream(new FileInputStream(new File(path)));
		List< String> jsoList = new ArrayList<String>();
		Book tempBook = null;
		String jsoString = null;
		while((tempBook = (Book) input.readObject())!=null){
			jsoString = objectToJson(tempBook);
			System.out.println(jsoString);
			jsoList.add(jsoString);
			System.out.println(jsoString);
		}
		input.close();
		return jsoList;
	}

}
